// Copyright 2010 Abhishek Kulkarni. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goprop

import (
	"fmt"
	"os"
	"flag"
)


/*
 The methods here enable parsing and checking of the debug-level
 specified by the user of the library, and reporting of debug
 statements per the selected debug-level.

 Possible debug levels are:

	none 		- no debug statements are printed
	propagator	- debug the propagators only
	cell		- debug the cells only
 	all		- enable complete debugging (highly verbose)
*/

// Possible debug levels
const (
	dbgNone = iota
	dbgPropagator
	dbgCell
	dbgAll
)

var (
	debugFlags = flag.String("debug", "none", "set debug level (options: none, propagator, cell, all)")
	debugLevel = dbgNone
)

// set debug level per the command-line option specified by the user
func setDebugLevel(dbgLevel string) int {
	switch dbgLevel {
	case "none":
		debugLevel = dbgNone
	case "propagator":
		debugLevel = dbgPropagator
	case "cell":
		debugLevel = dbgCell
	case "all":
		debugLevel = dbgAll
	default:
		debugLevel = dbgNone
	}
	return debugLevel
}

// function to report a debug statement depending on the debug level
func Debug(level int, format string, a ...interface{}) (n int, errno os.Error) {
	switch debugLevel {
	case dbgNone:
	case dbgAll:
		n, errno = fmt.Printf(format, a...)
		return n, errno
	default:
		if level == debugLevel {
			n, errno = fmt.Printf(format, a...)
			return n, errno
		}
	}

	return 0, nil
}

// function to report an error
func Error(format string, a ...interface{}) (n int, errno os.Error) {
	n, errno = fmt.Printf("error: " + format, a...)
	return n, errno
}
